var Singleton = (function(){
    var instance = null;
    
    function getDomainName(){
        var isDev = true;
        if (isDev) {
            var devDomain = window.location.href;
            this.getDomainName = devDomain.substring(0, devDomain.lastIndexOf("/"));
        } else {
            this.getDomainName =  window.location.origin;
        }
    };
    
    return{
        getInstance : function(){
            if(!instance){
                instance = new getDomainName() ;
            }
            return instance ;
        }
    } ;
})() ;

var DomainName = Singleton.getInstance().getDomainName;

  
function createTemplate(templateId,templateData){
    var source   = $("#"+templateId).html();
    var template = Handlebars.compile(source);
    return template(templateData);
}

function renderTemplate(htmlId,templateId,templateData){
    var source   = $("#"+templateId).html();
    var template = Handlebars.compile(source);
    $("#"+htmlId).html(template(templateData));
}




function renderHead() {
    var headerUrl = DomainName + "/header";
    $.ajax({
        url : headerUrl,
        type : "get",
        cache : false,
        timeout : 60 * 1000,
        dataType : "json",
        success : function(data) {
            console.info(data);
            $("#Head").html(createTemplate("HeadTemp", data));
        },
        error : function(data) {
            $("#Head").html(createTemplate("HeadTemp"));
            console.info("error");
        }
    });
}



function renderFooter() {
    var headerUrl = DomainName + "/footer";
    $.ajax({
        url : headerUrl,
        type : "get",
        cache : false,
        timeout : 60 * 1000,
        dataType : "json",
        success : function(data) {
            console.info(data);
            $("#Footer").html(createTemplate("FooterTemp"));
        },
        error : function(data) {
            console.info("error");
        }
    });
}


var ModelType = {
    large : {tempId:"LModal",divId:"largeModal"},
    normal : {tempId:"NModal",divId:"normalModal"},
    small : {tempId:"SModal",divId:"smallModal"},
    confirm : {tempId:"CModal",divId:"confirmModal"},
    loading:{tempId:"Loding",divId:"lodingModal"}
}; 

var confirmModalResult = -1;
var ModalDialog = (function(){
    
    var common = "common";
    var showDivId = "";
    
    
    function showModel(modal, data) {
        var divId = modal.divId;
        var tempId = modal.tempId;
        
        renderTemplate(common, tempId, data);
        

        if (tempId == "Loding") {
            $('#' + divId).modal({
                backdrop : 'static',
                keyboard : false
            });
        } else if (tempId == "CModal") {
            confirmModalResult = -1;
            $('#' + divId).modal("show");
        } else {
            $('#' + divId).modal("show");
        }

        
        this.showDivId = divId; 
    };

    function hideModel(){

        if (this.showDivId == "") {
            return;
        }

       $('#'+ this.showDivId).modal('hide');
       this.showDivId="";
    };
    
    function confirmChoose(choose) {
        confirmModalResult = choose;
    };
        
    return{
        show : function(modal, data){
            showModel(modal, data);
        },
        
        hide : function(){
            hideModel();
        },
        
        confirm : function(choose){
            confirmChoose(choose);
        }

    } ;
})();

var AlertType = {
    alertSuccess : {tempId:"NoticeSuccess",divId:"alertSuccess"},
    alertInfo : {tempId:"NoticeInfo",divId:"alertInfo"},
    alertDanger : {tempId:"NoticeDanger",divId:"alertDanger"},
    alertWarning : {tempId:"NoticeWarning",divId:"alertWarning"},
}; 

var AlertDialog = (function(){
    
    var common = "notify";
    var showDivId = "";
    var hideTime = 3000;
    
    function showAlert(modal, data) {
        var divId = modal.divId;
        var tempId = modal.tempId;
        
        if (data==undefined) {
            data = new Object();
            if (tempId == "NoticeSuccess") {
                data.info = "Success!";
            } else if (tempId == "NoticeInfo") {
                data.info = "Info!";
            } else if (tempId == "NoticeDanger") {
                data.info = "Danger!";
            } else if (tempId == "NoticeWarning") {
                data.info = "Warning!";
            }

        }
        
        renderTemplate(common,tempId,data);
        
        var headHeight = $("#Head").find("#MenuHead").height();
        var headWidth = $("#Head").find("#MenuHead").width();
        
        var offsetTop = headHeight  + 20;
        var offsetLeft = $("#" + divId).width()/2;
        
        $("#"+ divId).css({
            "opacity":0.75,
            "position":"fixed",
            "font-size":18,
            "z-index":2,
            "top":offsetTop,
            "left":offsetLeft
            });
        
        this.showDivId = divId; 
        setTimeout(hideAlert,hideTime);
    };

    function hideAlert(){
        $("#" + this.showDivId).alert('close');
    };
        
    return{
        show : function(modal, data){
            showAlert(modal, data);
        }
    } ;
})();



function init(){
    //renderHead();
    //renderFooter();
}